package org.yxs.es.schedu;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Random;
import java.util.UUID;

/**
 * @author: y-xs
 * @date: 2021/08/26 16:55
 * @description: 模拟定时收集日志
 */
@EnableScheduling
@Component
public class LogstashLogbackAggregation {

    private static final Logger LOGGER = LoggerFactory.getLogger(LogstashLogbackAggregation.class);
    private static final Random RANDOM = new Random();

    @Scheduled(cron = "0/3 * * * * ?")
    public void logbackMdc() {
        MDC.put("code", UUID.randomUUID().toString());
        MDC.put("createTime", LocalDateTime.now().toString());
        MDC.put("content", "logstash日志采集错误" + LocalDateTime.now());
        LOGGER.info("logstash日志采集，当前时间【{}】", LocalDateTime.now());
        MDC.clear();
//        System.out.println(String.format("logstash日志采集，当前时间【%s】", LocalDateTime.now()));
    }

}
